// main_screen.c
#include "main_screen.h"

// 定义主界面控件
static lv_obj_t *main_cont; // 主容器
static lv_obj_t *title_label;
static lv_obj_t *ticket_query_btn;
static lv_obj_t *ticket_purchase_btn;
static lv_obj_t *order_view_btn;
static lv_obj_t *weather_btn;
static lv_obj_t *customer_service_btn;
static lv_obj_t *ret_login_btn ;

// 按钮样式
static lv_style_t btn_style;
static lv_style_t btn_pressed_style;


const char * pay_png = "S:/userdata/zjy/pay.png";
const char * orderview_png = "S:/userdata/zjy/orderview.png";
const char * weather_png = "S:/userdata/zjy/weather.png";
const char * customer_png = "S:/userdata/zjy/customer.png";


// const char * pay_png = "S:/zjy/pay.png";
// const char * orderview_png = "S:/zjy/orderview.png";
// const char * weather_png = "S:/zjy/weather.png";
// const char * customer_png = "S:/zjy/customer.png";

//给容器添加图片

void  add_png_to_form(lv_obj_t *from,const char * png)
{

// 检查导航图片文件是否存在
    FILE *pay_file = fopen(png + 2, "r"); // 跳过"S:"前缀
    if (pay_file) {
    fclose(pay_file);
        LV_LOG_USER("Navigation PNG file found: %s", png);
    } else {
        LV_LOG_ERROR("Navigation PNG file not found: %s", png);
        // 可以设置一个默认图片或显示错误信息
    }
    
    lv_obj_t * pay_img = lv_img_create(from);
    lv_img_set_src(pay_img ,png);
    lv_img_set_zoom(pay_img , 280);
    lv_obj_center(pay_img);

}

static void return_Login(void)
{
     //返回主界面
    lv_scr_load(scr_login);
    //清除输入框内容
    lv_textarea_set_text(user_ta, "");
    lv_textarea_set_text(pwd_ta, "");   
}
// 初始化按钮样式
static void btn_style_init(void)
{
    lv_style_init(&btn_style);
    lv_style_set_bg_color(&btn_style, lv_palette_main(LV_PALETTE_BLUE));
    lv_style_set_bg_opa(&btn_style, LV_OPA_80);
    lv_style_set_radius(&btn_style, 10);
    lv_style_set_border_width(&btn_style, 2);
    lv_style_set_border_color(&btn_style, lv_color_white());
    lv_style_set_text_color(&btn_style, lv_color_white());
    lv_style_set_text_font(&btn_style, font_chinese);
    
    lv_style_init(&btn_pressed_style);
    lv_style_set_bg_color(&btn_pressed_style, lv_palette_main(LV_PALETTE_RED));
    lv_style_set_bg_opa(&btn_pressed_style, LV_OPA_80);
}

// 按钮事件处理
static void main_btn_event_handler(lv_event_t *e)
{
    lv_event_code_t code = lv_event_get_code(e);
    lv_obj_t *btn = lv_event_get_target(e);
    
    if(code == LV_EVENT_CLICKED) 
    {
        // if(btn == ticket_query_btn) 
        // {
        //     show_ticket_query_screen();
        // } 
        // else 
        if(btn == ticket_purchase_btn) 
        {
            show_ticket_purchase_screen();
        } 
        else if(btn == order_view_btn) 
        {
            show_order_view_screen();
        } 
        else if(btn == weather_btn) 
        {
            show_weather_screen();
        } 
        else if(btn == customer_service_btn) 
        {
            show_customer_service_screen();
        }
        else if (btn == ret_login_btn)
        {
            return_Login();
        }
        
    }
}

// 初始化主界面
void init_main_screen(void)
{
    
    // 创建主容器
    // main_cont = lv_obj_create(scr_main);
    // lv_obj_set_size(main_cont, LV_PCT(100), LV_PCT(100));
    // lv_obj_center(main_cont);
    // lv_obj_set_style_bg_opa(main_cont, LV_OPA_TRANSP, 0);
    // lv_obj_set_style_border_opa(main_cont, LV_OPA_TRANSP, 0);
    
    // 初始化按钮样式
    btn_style_init();
    
    // 创建标题
    // title_label = lv_label_create(main_cont);
    // lv_label_set_text(title_label, "购票系统");
    // lv_obj_set_style_text_font(title_label, font_chinese, 0);
    // lv_obj_set_style_text_color(title_label, lv_color_white(), 0);
    // lv_obj_align(title_label, LV_ALIGN_TOP_MID, 0, 20);
    
    // // 创建功能按钮
    // ticket_query_btn = lv_btn_create(main_cont);
    // lv_obj_add_style(ticket_query_btn, &btn_style, 0);
    // lv_obj_add_style(ticket_query_btn, &btn_pressed_style, LV_STATE_PRESSED);
    // lv_obj_set_size(ticket_query_btn, 200, 50);
    // lv_obj_align(ticket_query_btn, LV_ALIGN_TOP_MID, 0, 80);
    // lv_obj_add_event_cb(ticket_query_btn, main_btn_event_handler, LV_EVENT_CLICKED, NULL);
    
    // lv_obj_t *query_label = lv_label_create(ticket_query_btn);
    // lv_label_set_text(query_label, "票务查询");
    // lv_obj_center(query_label);
    // lv_obj_set_style_text_font(query_label, font_chinese, 0);
    
    // 创建主容器 - 不再隐藏
    lv_obj_t *scr_main_form = lv_obj_create(scr_main);
    lv_obj_set_size(scr_main_form, LV_PCT(100), LV_PCT(98)); // 全屏
    lv_obj_align(scr_main_form, LV_ALIGN_TOP_MID, 0, 50);
    lv_obj_set_style_bg_opa(scr_main_form, LV_OPA_TRANSP, 0);
    lv_obj_set_style_border_width(scr_main_form, 0, 0);
    // 确保不隐藏
    #if LVGL_VERSION_MAJOR >= 8
        lv_obj_clear_flag(scr_main_form, LV_OBJ_FLAG_HIDDEN);
    #else
        lv_obj_set_hidden(scr_main_form, false);
    #endif
    
    // 创建左侧容器
    lv_obj_t *left_container = lv_obj_create(scr_main_form);
    lv_obj_set_size(left_container, LV_PCT(50), LV_PCT(100));
    lv_obj_align(left_container, LV_ALIGN_TOP_LEFT, 0, 0);
    lv_obj_set_style_bg_opa(left_container, LV_OPA_TRANSP, 0);
    lv_obj_set_style_border_width(left_container, 0, 0);
    lv_obj_set_flex_flow(left_container, LV_FLEX_FLOW_COLUMN);
    lv_obj_set_flex_align(left_container, LV_FLEX_ALIGN_SPACE_EVENLY, 
                          LV_FLEX_ALIGN_CENTER, LV_FLEX_ALIGN_CENTER);
    lv_obj_set_style_pad_all(left_container, 10, 0);

    // 创建上方容器 (pay)
    lv_obj_t *pay = lv_obj_create(left_container);
    lv_obj_set_size(pay, LV_PCT(100), LV_PCT(48));
    lv_obj_set_style_bg_opa(pay, LV_OPA_10, 0);
    lv_obj_set_style_bg_color(pay, lv_color_make(255, 250, 250), 0);
    lv_obj_set_style_border_width(pay, 0, 0);
    lv_obj_set_style_radius(pay, 15, 0);
    lv_obj_set_style_pad_all(pay, 10, 0);
    lv_obj_set_scroll_dir(pay ,LV_DIR_NONE);
        
    //添加图片给pay
    add_png_to_form(pay,pay_png);
    



    // 创建下方容器 (find_d)
    lv_obj_t *find_d = lv_obj_create(left_container);
    lv_obj_set_size(find_d, LV_PCT(100), LV_PCT(48));
    lv_obj_set_style_bg_opa(find_d, LV_OPA_50, 0);
    lv_obj_set_style_border_width(find_d, 0, 0);
    lv_obj_set_style_radius(find_d, 15, 0);
    lv_obj_set_style_pad_all(find_d, 10, 0);
    lv_obj_set_scroll_dir(find_d ,LV_DIR_NONE);
    //添加图片给find
    add_png_to_form(find_d,orderview_png);
    
    // 创建右侧容器
    lv_obj_t *right_container = lv_obj_create(scr_main_form);
    lv_obj_set_size(right_container, LV_PCT(50), LV_PCT(100));
    lv_obj_align(right_container, LV_ALIGN_TOP_RIGHT, 0, 0);
    lv_obj_set_style_bg_opa(right_container, LV_OPA_TRANSP, 0);
    lv_obj_set_style_border_width(right_container, 0, 0);
    lv_obj_set_flex_flow(right_container, LV_FLEX_FLOW_COLUMN);
    lv_obj_set_flex_align(right_container, LV_FLEX_ALIGN_SPACE_EVENLY, 
                          LV_FLEX_ALIGN_CENTER, LV_FLEX_ALIGN_CENTER);
    lv_obj_set_style_pad_all(right_container, 10, 0);

    // 创建天气容器 (wea)
    lv_obj_t *wea = lv_obj_create(right_container);
    lv_obj_set_size(wea, LV_PCT(100), LV_PCT(48));
    lv_obj_set_style_bg_opa(wea, LV_OPA_90, 0);
    lv_obj_set_style_border_width(wea, 0, 0);
    lv_obj_set_style_radius(wea, 15, 0);
    lv_obj_set_style_pad_all(wea, 10, 0);
    lv_obj_set_scroll_dir(wea ,LV_DIR_NONE);
     //添加图片给wea
    add_png_to_form(wea,weather_png);
   
    // 创建客服容器 (conet)
    lv_obj_t *conet = lv_obj_create(right_container);
    lv_obj_set_size(conet, LV_PCT(100), LV_PCT(48));
    lv_obj_set_style_bg_opa(conet, LV_OPA_90, 0);
    lv_obj_set_style_border_width(conet, 0, 0);
    lv_obj_set_style_radius(conet, 15, 0);
    lv_obj_set_style_pad_all(conet, 10, 0);
    lv_obj_set_scroll_dir(conet ,LV_DIR_NONE);
    //添加图片给conet
    add_png_to_form(conet,customer_png);

    // 购票按钮
    ticket_purchase_btn = lv_btn_create(pay);
    // lv_obj_add_style(ticket_purchase_btn, &btn_style, 0);
    lv_obj_add_style(ticket_purchase_btn, &btn_pressed_style, LV_STATE_PRESSED);
    lv_obj_set_size(ticket_purchase_btn, 450, 250);
    lv_obj_center(ticket_purchase_btn);
    lv_obj_add_event_cb(ticket_purchase_btn, main_btn_event_handler, LV_EVENT_CLICKED, NULL);
    lv_obj_set_style_pad_all(ticket_purchase_btn , 0 ,0);
    lv_obj_set_style_opa(ticket_purchase_btn , LV_OPA_TRANSP, 0);
    lv_obj_add_flag(ticket_purchase_btn, LV_OBJ_FLAG_EVENT_BUBBLE);
    // lv_obj_t *purchase_label = lv_label_create(ticket_purchase_btn);
    // lv_label_set_text(purchase_label, "购票");
    // lv_obj_center(purchase_label);
    // lv_obj_set_style_text_font(purchase_label, font_chinese, 0);
    
    // 订单查询按钮
    order_view_btn = lv_btn_create(find_d);
    // lv_obj_add_style(order_view_btn, &btn_style, 0);
    lv_obj_add_style(order_view_btn, &btn_pressed_style, LV_STATE_PRESSED);
    lv_obj_set_size(order_view_btn, 450, 250);
    lv_obj_center(order_view_btn);
    lv_obj_add_event_cb(order_view_btn, main_btn_event_handler, LV_EVENT_CLICKED, NULL);
    lv_obj_set_style_pad_all(order_view_btn , 0 ,0);
    lv_obj_set_style_opa(order_view_btn , LV_OPA_TRANSP, 0);
    lv_obj_add_flag(order_view_btn, LV_OBJ_FLAG_EVENT_BUBBLE);


    // lv_obj_t *order_label = lv_label_create(order_view_btn);
    // lv_label_set_text(order_label, "查看订单");
    // lv_obj_center(order_label);
    // lv_obj_set_style_text_font(order_label, font_chinese, 0);
    
    // 天气查询按钮
    weather_btn = lv_btn_create(wea);
    // lv_obj_add_style(weather_btn, &btn_style, 0);
    lv_obj_add_style(weather_btn, &btn_pressed_style, LV_STATE_PRESSED);
    lv_obj_set_size(weather_btn,450, 250);
    lv_obj_center(weather_btn);
    lv_obj_add_event_cb(weather_btn, main_btn_event_handler, LV_EVENT_CLICKED, NULL);
    lv_obj_set_style_pad_all(weather_btn , 0 ,0);
    lv_obj_set_style_opa(weather_btn , LV_OPA_TRANSP, 0);
    lv_obj_add_flag(weather_btn, LV_OBJ_FLAG_EVENT_BUBBLE);
    
    // lv_obj_t *weather_label = lv_label_create(weather_btn);
    // lv_label_set_text(weather_label, "天气查询");
    // lv_obj_center(weather_label);
    // lv_obj_set_style_text_font(weather_label, font_chinese, 0);
    
    // 联系客服按钮
    customer_service_btn = lv_btn_create(conet);
    lv_obj_add_style(customer_service_btn, &btn_style, 0);
    lv_obj_add_style(customer_service_btn, &btn_pressed_style, LV_STATE_PRESSED);
    lv_obj_set_size(customer_service_btn, 450, 250);
    lv_obj_center(customer_service_btn);
    lv_obj_add_event_cb(customer_service_btn, main_btn_event_handler, LV_EVENT_CLICKED, NULL);
    lv_obj_set_style_pad_all(customer_service_btn , 0 ,0);
    lv_obj_set_style_opa(customer_service_btn , LV_OPA_TRANSP, 0);
    lv_obj_add_flag(customer_service_btn, LV_OBJ_FLAG_EVENT_BUBBLE);
    
    // lv_obj_t *service_label = lv_label_create(customer_service_btn);
    // lv_label_set_text(service_label, "联系客服");
    // lv_obj_center(service_label);
    // lv_obj_set_style_text_font(service_label, font_chinese, 0);

    // 创建返回登录界面按钮
    ret_login_btn = lv_btn_create(scr_main);
    lv_obj_add_style(ret_login_btn, &btn_style, 0);
    lv_obj_add_style(ret_login_btn, &btn_pressed_style, LV_STATE_PRESSED);
    lv_obj_set_size(ret_login_btn, 100, 50);
    lv_obj_align(ret_login_btn, LV_ALIGN_TOP_RIGHT, -10, 0);
    lv_obj_add_event_cb(ret_login_btn, main_btn_event_handler, LV_EVENT_CLICKED, NULL);
    lv_obj_set_style_pad_all(ret_login_btn , 0, 0);
    
    lv_obj_t *ret_login_label = lv_label_create(ret_login_btn);
    lv_label_set_text(ret_login_label, "返回登录");
    lv_obj_center(ret_login_label);
    lv_obj_set_style_text_font(ret_login_label, font_chinese, 0);

    
    
    // // 添加一些调试输出
    // printf("Main screen initialized\n");
    
    // 强制刷新屏幕
    lv_refr_now(NULL);
}

// // 票务查询界面
// void show_ticket_query_screen(void)
// {
//     // 实现票务查询界面
//     // 可以根据班次号、时间、起点终点查询
// }




// 天气查询界面
void show_weather_screen(void)
{
    // 实现天气查询界面
    // 显示近七日天气
    // 可以直接调用json_process.c中的功能
    ui(); // 初始化天气界面
    jsonDataProcessing(); // 获取天气数据
}
